using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using MetaObjects.Contracts;

namespace MetaObjects.Core
{
	public class DataTableProvider : MetaEntityProvider<DataTable>
	{
		public IMetaFieldProvider<DataColumn> FieldProvider { get; private set; }

		public DataTableProvider()
			: this(new DataColumnProvider())
		{
		}

		public DataTableProvider(IMetaFieldProvider<DataColumn> fieldProvider)
		{
			FieldProvider = fieldProvider;
		}

		protected override IMetaEntity GetMetaEntity(DataTable entity)
		{
			var fields = entity.Columns.OfType<DataColumn>().Select(FieldProvider.FieldFactory);
			var metaEntity = new MetaDataRow(entity.TableName, entity.TableName, entity.NewRow, fields);
			return metaEntity;
		}
	}
}